package com.cabinet.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.base.util.PageUtils;
import com.cabinet.easy.util.Page;
import com.cabinet.web.entity.Cabinet;
import com.cabinet.web.entity.Lattice;
import com.cabinet.web.entity.Seller;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;

import cn.com.cabinet.dto.LatticeDto;
import cn.com.cabinet.service.LatticeServiceImpl;
import cn.com.cabinet.service.SellerServiceImpl;
import cn.com.easy.dto.MessageDTO;
import cn.com.easy.dto.PageRequestParamDTO;
import cn.com.easy.utils.CookieUtils;
import cn.com.easy.utils.FastJSONUtils;
import cn.com.easy.utils.ResponseOutputUtils;

@Controller
@RequestMapping("/seller/LatticeManager")
public class SellerLatticeController {
	
	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	@Autowired
	private LatticeServiceImpl LatticeServiceImpl;
	
	@RequestMapping("/lattice")
	public String redirect(String cid,ModelMap modelMap){
		System.out.println(cid);
		modelMap.addAttribute("cid", cid);
		return "/latticeManager/index";
	}
	
	@RequestMapping("/latticeManager/list")
	@ResponseBody
	public void getCabinetByNoneMark(HttpServletRequest req, HttpServletResponse res, PageRequestParamDTO paramDto, Integer key, String status) throws Exception{
		System.out.println(status);
		String pfCount = CookieUtils.getCookieValue(req, "pfCount");
		Seller s = sellerServiceImpl.getSellerBysAccount(pfCount);
		try {
			int thispage = (paramDto.getPageNumber()-1)*10;
			List<LatticeDto> latDtoList= LatticeServiceImpl.getLatticeList(status);
			int totalcount = latDtoList.size();
			Paginator p = new Paginator(thispage, paramDto.getPageSize(), totalcount);
			PageList<LatticeDto> pageObject = new PageList<>(latDtoList, p);
			Page<LatticeDto> page = PageUtils.getPage(pageObject);
			
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			//logger.error("会员管理列表错误：", ex);
			ResponseOutputUtils.renderText(res, FastJSONUtils.toJsonString(MessageDTO.newInstance("", false, "会员管理列表错误")));
		}
		
	}
	
	@RequestMapping("/lattice/one")
	@ResponseBody
	public LatticeDto getLatticeOne(Long id){
		return LatticeServiceImpl.getLatticeOne(id);
		
	}
	
	@RequestMapping("/submit")
	@ResponseBody
	public int updateLattice(Long latticeId,Long commoditiesId,String salePrice){
		Lattice la = LatticeServiceImpl.getLatticeById(latticeId);
		la.setSalePrice(salePrice);
		la.setCommoditiesId(commoditiesId);
		LatticeServiceImpl.updateLattice(la);
		return 0;
		
		
		
	}
	
	

}
